package cn.itcast.bos.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.base.SubArea;
import cn.itcast.bos.service.SubAreaService;
import cn.itcast.bos.utils.PinYin4jUtils;
import cn.itcast.bos.web.action.common.BaseAction;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class SubAreaAction extends BaseAction<SubArea> {

	@Autowired
	private SubAreaService subAreaService;
	
	//分区分页条件查询
	@Action(value="subArea_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		Pageable pageable = new PageRequest(page - 1, rows);
		
		//调用service层
		Page<SubArea> page = subAreaService.pageQuery(pageable);
		
		pushPageDataToValueStack(page);

		return SUCCESS;
	}
	
	//接收上传文件
	private File file;
	private String fileFileName;
	private String fileContentType;
	
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}
	public void setFile(File file) {
		this.file = file;
	}
	
	//批量区域数据导入
	@Action(value="subArea_batchImport")  //文件上传不需要result
	public String BatchImport() throws IOException, IOException{
		//利用POI处理一键上传的操作
		//编写解析file的代码逻辑
		List<SubArea> sList = new ArrayList<SubArea>();
		//创建一个表格对象,用于接收解析得到的Excel表格中的sheet工作簿
		Sheet sheet = null;
		System.out.println(fileFileName);
		if(fileFileName.endsWith(".xls")){
			//基于.xls文件格式解析HSSF
			//1.加载Excel文件对象
			HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(file));
			//2.读取一个sheet
			sheet = (HSSFSheet)hssfWorkbook.getSheetAt(0);
		}else if(fileFileName.endsWith(".xlsx")){
			//基于.xlsx文件格式解析HSSF
			//1.加载Excel文件对象
			XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream(file));
			//2.读取一个sheet
			sheet = (XSSFSheet)xssfWorkbook.getSheetAt(0);
		}
		//3.读取sheet中的每一行
		for (Row row : sheet) {
			//一行数据,对应一条区域对象
			if (row.getRowNum() == 0) {
				//第一行,跳过
				continue;
			}
			//跳过空行
			if(row.getCell(0) ==null
					|| StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
				continue;
			}

			
			//创建SubArea对象用来封装每一条数据
			SubArea subArea = new SubArea();
 			subArea.setId(row.getCell(0).getStringCellValue());
//			Area area = new Area();
//			subArea.setArea(area);
//			subArea.getArea().setProvince(row.getCell(1).getStringCellValue());
//			subArea.getArea().setCity(row.getCell(2).getStringCellValue());
//			subArea.getArea().setDistrict(row.getCell(3).getStringCellValue());
			subArea.setKeyWords(row.getCell(4).getStringCellValue());
			subArea.setStartNum(row.getCell(5).getStringCellValue());
			subArea.setEndNum(row.getCell(6).getStringCellValue());
			subArea.setSingle(row.getCell(7).getStringCellValue().charAt(0));
			subArea.setAssistKeyWords(row.getCell(8).getStringCellValue());
			
			
			sList.add(subArea);
			
		}
		
		System.out.println(sList);
		
		//调用业务层
		subAreaService.BatchImport(sList);
		
		return NONE;
	}

}
